package cn.zhouchao.pet.service.impl;

import cn.zhouchao.basic.util.*;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.domain.Shop;
import cn.zhouchao.org.mapper.EmployeeMapper;
import cn.zhouchao.org.mapper.ShopMapper;
import cn.zhouchao.pet.domain.SearchMasterMsg;
import cn.zhouchao.pet.domain.SearchMasterMsgAuditLog;
import cn.zhouchao.pet.dto.PublishDto;
import cn.zhouchao.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zhouchao.pet.mapper.SearchMasterMsgMapper;
import cn.zhouchao.pet.service.ISearchMasterMsgService;
import cn.zhouchao.basic.service.impl.BaseServiceImpl;
import cn.zhouchao.user.domain.Logininfo;
import cn.zhouchao.user.domain.User;
import cn.zhouchao.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouchao
 * @since 2023-03-16
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper logMapper;

    @Override
    public void publish(PublishDto dto) {
        //参数校验（dto已经完成）
        //保存数据到数据库
        SearchMasterMsg msg = saveMsg(dto);
        //百度AI审核(审核不通过，添加到日志)
        if(!BaiduAuditUtils.TextCensor(dto.getName())){
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(msg.getId());
            log.setState(0);//0失败,1才是成功
            log.setAuditId(1L);
            log.setAuditTime(new Date());
            log.setNote("宠物名称违规:"+ dto.getName());
            logMapper.add(log);
        }
        //todo 人工审核
        //50公里范围寻主
        Point shop = DistanceUtil.getPoint(dto.getAddress());//获取卖家店铺地址经纬度
        List<Shop> shops = shopMapper.findAll();
        Shop nearestShop = DistanceUtil.getNearestShop(shop, shops);
        //找到
        if (nearestShop != null){
            msg.setShopId(nearestShop.getId());
            super.add(msg);
        }
        //todo 没找到进去寻主池
    }

    /**
     * 保存数据
     * @param dto
     * @return
     */
    private SearchMasterMsg saveMsg(PublishDto dto) {
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setName(dto.getName());
        msg.setAge(dto.getAge());
        msg.setGender(dto.getGender());
        msg.setCoatColor(dto.getCoatColor());
        msg.setPetType(dto.getPetType());
        msg.setPrice(dto.getPrice());
        msg.setAddress(dto.getAddress());
        msg.setState(0);//待处理状态
        msg.setTitle(dto.getTitle());
        Logininfo logininfo = LoginContext.getLogininfo();
        Employee emp = new Employee();
        User user = new User();
        msg.setUserId(userMapper.findByLogininfoId(logininfo.getId()).getId());//这里先获取登陆者的userId
        super.add(msg);//添加到数据库
        return msg;
    }
}
